package com.lw.service.impl;

import com.lw.common.vo.MenuMeta;
import com.lw.common.vo.MenuVo;
import com.lw.service.TbRoleMenuService;
import com.lw.service.TbRoleService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lw.entity.TbMenu;
import com.lw.mapper.TbMenuMapper;
import com.lw.service.TbMenuService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务层实现。
 *
 * @author lw
 * @since 2025年04月30日 21:22:12
 */
@Service
public class TbMenuServiceImpl extends ServiceImpl<TbMenuMapper, TbMenu> implements TbMenuService {

    @Resource
    private TbRoleMenuService tbRoleMenuService;


    @Override
    public List<TbMenu> getMenuTree(Integer roleId) {
        List<TbMenu> allMenus = tbRoleMenuService.getMenuByRoleId(roleId);// 获取所有菜单
        return buildMenuTree(allMenus, null);
    }

    @Override
    public List<MenuVo> convertToMenuVoList(List<TbMenu> menuTreeList) {
        List<MenuVo> menuVoList = new ArrayList<>();
        if (menuTreeList == null) {
            return menuVoList;
        }
        for (TbMenu menu : menuTreeList) {
            MenuVo menuVo = convertTreeToMenuVo(menu);
            menuVoList.add(menuVo);
        }
        return menuVoList;
    }
    private  MenuVo convertTreeToMenuVo(TbMenu menu) {
        if (menu == null) {
            return null;
        }
        MenuVo menuVo = new MenuVo();
        BeanUtils.copyProperties(menu, menuVo);
        menuVo.setVisible(true);//先设置看效果
        menuVo.setPath(menu.getRoutePath());//先设置看效果
        MenuMeta menuMeta = new MenuMeta();
        BeanUtils.copyProperties(menu, menuMeta);
        menuMeta.setAlwaysShow(menu.getAlwaysShow() > 0);
        menuMeta.setHidden(menu.getHidden() > 0);
        menuMeta.setKeepAlive(menu.getKeepAlive() > 0);
        menuVo.setMeta(menuMeta);
        List<TbMenu> children = menu.getChildren();
        if (children != null && !children.isEmpty()) {
            List<MenuVo> childrenVo = convertToMenuVoList(children);
            if(childrenVo.isEmpty()){
                menuVo.setVisible(false);
            }
            menuVo.setChildren(childrenVo);
        }
        return menuVo;
    }


    /**
     * 递归构建菜单树
     * @param allMenus 所有菜单列表
     * @param parentId 父菜单 ID，null 表示根菜单
     * @return 菜单树
     */
    private List<TbMenu> buildMenuTree(List<TbMenu> allMenus, Integer parentId) {
        List<TbMenu> menuTree = new ArrayList<>();
        for (TbMenu menu : allMenus) {
            if ((parentId == null && menu.getParentId() == null) ||
                    (parentId != null && parentId.equals(menu.getParentId()))) {
                List<TbMenu> children = buildMenuTree(allMenus, menu.getId());
                if (!children.isEmpty()) {
                    menu.setChildren(children);
                }
                menuTree.add(menu);
            }
        }
        return menuTree;
    }
}
